package com.gitee.huxing.course.core.datasource;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.gitee.huxing.course.core.annoation.DataSourceEnum;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description: 数据源配置管理
 * @ProjectName: JavaCourse
 * @Package: com.gitee.huxing.course.core.datasource
 * @ClassName: MyBatiesDataSourceConfiguration
 * @Author: huxing
 * @DateTime: 2021-09-16 上午11:05
 */
@Configuration
public class DataSourceConfiguration {

    /**
     * @Description: 配置主库数据源
     * @Author: huxing
     * @Date: 2021-05-22 18:51
     * @return: com.alibaba.druid.pool.DruidDataSource
     **/
    @Bean(name = "masterDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.master.druid")
    public DataSource mastDataSource(){
        return DruidDataSourceBuilder.create().build();
    }

    /**
     * @Description: 配置从库数据源
     * @Author: huxing
     * @Date: 2021-05-22 18:51
     * @return: com.alibaba.druid.pool.DruidDataSource
     **/
    @Bean(name = "slaveDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.slave.druid")
    public DataSource slaveDataSource(){
        return DruidDataSourceBuilder.create().build();
    }

    @Bean(name = "multipleDataSource")
    public DataSource multipleDataSource(@Qualifier("masterDataSource") DataSource masterDataSource,
                                         @Qualifier("slaveDataSource") DataSource slaveDataSource){
        MultipleDataSource multipleDataSource = new MultipleDataSource();
        Map< Object, Object > targetDataSources = new HashMap<>();
        targetDataSources.put(DataSourceEnum.MASTER.getValue(), masterDataSource);
        targetDataSources.put(DataSourceEnum.SLAVE.getValue(), slaveDataSource);
        // 添加多数据源
        multipleDataSource.setTargetDataSources(targetDataSources);
        // 设置默认数据源
        multipleDataSource.setDefaultTargetDataSource(masterDataSource);
        // 默认建立数据源连接
        try {
            masterDataSource.getConnection();
        } catch (Exception ex){
            ex.printStackTrace();
        }
        return multipleDataSource;
    }

    @Bean
    @ConfigurationProperties(prefix = "mybatis.configuration")
    public org.apache.ibatis.session.Configuration globalConfiguration(){
        return new org.apache.ibatis.session.Configuration();
    }

    @Bean("sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("multipleDataSource") DataSource dataSource,
                                               org.apache.ibatis.session.Configuration configuration) throws Exception {
        SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
        sqlSessionFactory.setDataSource(dataSource);
        sqlSessionFactory.setConfiguration(configuration);
        sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().
                getResources("classpath:/mapper/*Mapper.xml"));
        return sqlSessionFactory.getObject();
    }
}
